package ExpressThree;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Interface extends JFrame implements ActionListener {
    ControlUntils c;

    LinkList<JButton>btn_dct,btn_coct,btn_chct;
    JPanel panel_dct,panel_coct,panel_chct;
    String[]name1={"添加进程","终止进程","添加设备","删除设备","设备独立性","退出"};
    JMenuBar jMenuBar;
    JMenu jMenu;
    JMenuItem[] jMenuItem;
    static int pid=10000;


    public Interface(){
        c=new ControlUntils();
        panel_dct=new JPanel(new GridLayout(20,1));
        panel_coct=new JPanel(new GridLayout(10,1));
        panel_chct=new JPanel(new GridLayout(5,1));


        panel_dct.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        panel_coct.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        panel_chct.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));

        change();

        initMenu();

        add(panel_dct);
        add(panel_coct);
        add(panel_chct);
        setLayout(new GridLayout(1,3));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(100,100,1000,600);
    }
    void initMenu(){
        jMenuBar=new JMenuBar();
        jMenu=new JMenu("设置");
        jMenuItem=new JMenuItem[name1.length];
        for(int i=0;i<jMenuItem.length;i++){
            jMenuItem[i]=new JMenuItem(name1[i]);
            jMenuItem[i].addActionListener(this);
            jMenu.add(jMenuItem[i]);
        }
        jMenuBar.add(jMenu);
        setJMenuBar(jMenuBar);
    }
    void change(){

        panel_coct.removeAll();
        panel_chct.removeAll();
        panel_dct.removeAll();

        btn_dct=new LinkList<>();
        btn_coct=new LinkList<>();
        btn_chct=new LinkList<>();

        panel_dct.revalidate();
        panel_chct.revalidate();
        panel_coct.revalidate();

        //初始化dct
        for(int i=0;i<c.getList_dct().size();i++){
            JButton button=new JButton(c.getList_dct().get(i).getIdentifyName()+" type: "+c.getList_dct().get(i).getType()+" current: "+c.getList_dct().get(i).getProcessNode());

            button.setFont(new Font("宋体",Font.BOLD,16));
            btn_dct.add(button);
            button.addActionListener(this);
            panel_dct.add(button);
        }
        //初始化coct
        for(int i=0;i<c.getList_coct().size();i++){
            JButton button=new JButton(c.getList_coct().get(i).getIdentifyName()+" "+c.getList_coct().get(i).getProcessNode());

            button.setFont(new Font("宋体",Font.BOLD,16));
            btn_coct.add(button);
            button.addActionListener(this);
            panel_coct.add(button);
        }
        //初始化chct
        for(int i=0;i<c.getList_chct().size();i++){
            JButton button=new JButton(c.getList_chct().get(i).getIdentifyName()+" "+c.getList_chct().get(i).getProcessNode());
            button.setFont(new Font("宋体",Font.BOLD,16));
            btn_chct.add(button);
            button.addActionListener(this);
            panel_chct.add(button);
        }

        /*panel_dct.revalidate();
        panel_chct.revalidate();
        panel_coct.revalidate();*/

    }
    public static void main(String[] args) {
        new Interface();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case "添加进程":
                String str1 = (String) JOptionPane.showInputDialog(this, "请输入申请设备名称",
                        "设备名称",
                        JOptionPane.INFORMATION_MESSAGE, null, null, null);
                if (!(str1 == null)) {
                    c.addProcess(str1, new ProcessNode(String.valueOf(pid)));
                    pid++;
                    change();
                }
                break;
            case "终止进程":
                String str3 = (String) JOptionPane.showInputDialog(this, "请输入进程名称",
                        "终止进程",
                        JOptionPane.WARNING_MESSAGE, null, null, null);
                if (!(str3 == null)) {
                    c.removeProcess(new ProcessNode(str3));
                    change();
                }
                break;
            case "添加设备": {
                String str4 = (String) JOptionPane.showInputDialog(this, "请输入新设备名称",
                        "设备名称",
                        JOptionPane.INFORMATION_MESSAGE, null, null, null);
                String str5 = (String) JOptionPane.showInputDialog(this, "请输入新设备类型",
                        "设备类型",
                        JOptionPane.INFORMATION_MESSAGE, null, null, null);
                if (!(str4 == null || str5 == null)) {
                    c.addDriver(str4, str5);
                    change();
                }

                break;
            }
            case "删除设备": {
                String str5 = (String) JOptionPane.showInputDialog(this, "请输入删除设备名称",
                        "设备名称",
                        JOptionPane.WARNING_MESSAGE, null, null, null);
                if (!(str5 == null)) {
                    c.removeDCT(str5);
                    change();
                }

                break;
            }
            case "设备独立性":
                String str6 = (String) JOptionPane.showInputDialog(this, "请输入申请设备类型",
                        "设备名称",
                        JOptionPane.INFORMATION_MESSAGE, null, null, null);
                if (!(str6 == null)) {
                    c.addProcess1(str6, new ProcessNode(String.valueOf(pid)));
                    pid++;
                    change();
                }
                break;
            case "退出":
                System.exit(0);
            default:

                for (int i = 0; i < btn_dct.size(); i++) {
                    if (e.getSource() == btn_dct.get(i)) {
                        JOptionPane.showMessageDialog(this,
                                c.getList_dct().get(i).toString(),
                                "消息",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                for (int i = 0; i < btn_coct.size(); i++) {
                    if (e.getSource() == btn_coct.get(i)) {
                        JOptionPane.showMessageDialog(this,
                                c.getList_coct().get(i),
                                "消息",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                for (int i = 0; i < btn_chct.size(); i++) {
                    if (e.getSource() == btn_chct.get(i)) {
                        JOptionPane.showMessageDialog(this,
                                c.getList_chct().get(i),
                                "消息",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                break;
        }
    }
}
